var web = web || {};
web.FormValidate = Class.create(); 
web.FormValidate.prototype = {
	initialize: function(elem, options) {
		var _this = this;
		this.settings = $.extend({
			num: 5,
			onChangeRate: function(level){},
			onClick: function(level){},
			half: false
		}, options);
				
		this._init();
	},
	_init: function() {
	},
	_checkEmail: function(val) {
		var _p = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]*)*@[a-zA-Z0-9\-]+([\.][a-zA-Z0-9\-]+)+$/;
		return this._checkPattern(_p, val);
	},
	_checkPattern: function(pattern, val) {
		if(pattern.test(val))
			return true;
		return false;
	},
	_textLimit: function(element) {
		var _this = this;
		element.rule.bytesEle.html(element.rule.bytes);
		element.obj.bind('keyup', {e:element}, function(event){
			var _e = event.data.e;
			_this._checkTextLimit(_e.obj, _e.rule.bytesEle, _e.rule.bytes);
		});
	},
	_auto: function(element) {
		var _val = element.obj.val();
		var _funcs = element.rule.auto.split('|');
		for (var i=0; i<_funcs.length; i++) {
			if (''==_val) break;
			switch(_funcs[i]) {
				case 'trim': _val = this._trim(_val);break;
				case 'dbc' : _val = this._caseSDC2DBC(_val);break;
				case 'lower': _val = this._toLower(_val);break;
				case 'upper': _val = this._toUpper(_val);break;
			}
		}
		return _val;
	},
	_inputTips: function(input) {
		input.bind({
			click:function() {
				var _self = $(this);
				if (_self.attr('tips')) {
					if (_self.val() == _self.attr('tips')) {
						_self.val('').removeClass('hint');
					}
				} else {
					_self.attr('tips', _self.val()).val('').removeClass('hint');
				}
			},
			blur:function() {
				var _self = $(this);
				if (''==_self.val()) {
					_self.addClass('hint').val(_self.attr('tips'));
				}
			}
		});
	},
	_checkTextLimit: function(element, show, num) {
		var _val = element.val();
		var _length = this._byteLength(_val);
		
		if (_length > num) {
			var _match;
			var _chars = _val.split("");
			var _byteLength = _sumLength = _cutLength = 0;
			for (var i=0; i<_chars.length; i++) {
				_match = _chars[i].match(/[^\x00-\x80]/g);
				_byteLength = (_chars[i].length + (!_match ? 0 : _match.length));
				
				_sumLength += _byteLength;
				if (i<num && _sumLength<=num) {
					_cutLength = i + 1;
				}
			}
			if(_val.length > _cutLength) {
				element.val( _val.substr(0, _cutLength));
			} else {
				element.val(_val);
			}
			
			show.html('0');
		} else {
			show.html(num-_length); 
		}
	},
	_byteLength : function(str){
		if (str == null) return 0;
		var _length = str.length;
		var _byteLen = 0;
		for(i=0; i<_length; i++) {
			if ((str.charCodeAt(i) & 0xff00) != 0) {
				_byteLen++;
			}
			_byteLen++;
		}
		return _byteLen;
	},
	_inStr : function(src, inc) {
		if (this._isEmpty(src) || this._isEmpty(inc)) return false;
		for (var i=0; i<src.length; i++) {
			if (inc.indexOf(src.charAt(i))==-1)
				return false;
		}
		return true;
	},
	_isEmpty : function(str) {
		str = this._trim(str);
		if(''==str)
			return true;
		return false;	
	},
	_trim : function(str) {
		return str.replace(/^\s*|\s*$/g, "");
	},
	_toLower : function(str) {
		return str.toLowerCase();
	},
	_toUpper : function(str) {
		return str.toUpperCase();
	},
	_caseSDC2DBC: function(str) {
		var _s = '';
		var _code;
		for (var i=0; i<str.length; i++) {
			_code = str.charCodeAt(i);
			if (_code >= 65281 && _code < 65373) {
				_s += String.fromCharCode(str.charCodeAt(i) - 65248);
			} else {
				_s += str.charAt(i);
			}
		}
		return _s;	
	}
};

;(function($) {
	$.fn.rating = function(options) {
		return this.each(function(){
			new web.Rating($(this), options);
		});
	}
})(jQuery);